package pers.w.chaptertalk.funs;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import pers.w.chaptertalk.R;

public abstract class WebBmpThread extends Thread implements Runnable {


    private List<BookInfo> bookInfos;

     protected WebBmpThread(List<BookInfo> bookInfos) {

        start =0;step =1;
        this.bookInfos = bookInfos;
    }
    protected WebBmpThread(List<BookInfo> bookInfos,int start,int step) {

        this.start =start;this.step =step;
        this.bookInfos = bookInfos;
    }
    public void setStop(boolean stop) {
        this.stop = stop;
    }

    private boolean stop = false;
    private int start,step;

    @Override
    public void run() {
        BookInfo bookinfo;
        for (int i = start; i < bookInfos.size(); i=i+step) {
            bookinfo = bookInfos.get(i);

            //没有图片，并且有图片uri信息
            while (null == bookinfo.getImage_bmp()) {
                if (stop) break;

                //没有图片uri信息,暂时为笔趣阁类网址
                if (bookinfo.getFieldValue(R.string.info_img).isEmpty()) {
                    if (!get_BQG_bookinfo(bookinfo)) continue;
                }
                try {
                    URL url = new URL(bookinfo.getFieldValue(R.string.info_img));
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setReadTimeout(5000);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    Bitmap bmp = BitmapFactory.decodeStream(is);
                    bookinfo.setImage_bmp(bmp);
                    if (bookinfo.getID() > 0) bookinfo.saveBmp();
                    if (null != bmp)
                        runOnUiThread(i);
                    is.close();
                } catch (IOException e) {
                    //e.printStackTrace();
                    Log.d(TAG, "run: "+ e.getMessage());
                }
            }

        }

    }
private static final String TAG ="web bmp thread";
    private boolean get_BQG_bookinfo(final BookInfo bookinfo) {

        final String uri = bookinfo.getFieldValue(R.string.info_uri);
        Document doc;
        try {
            doc = Jsoup.connect(uri).timeout(5000).get();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        if (Fun.isDebug()) Log.d("getChapter", uri + ": 开始获取章节");
        assert uri != null;
        Elements result_list;

        result_list = doc.select("img");
        if (null != result_list && result_list.size() > 0 && bookinfo.getFieldValue(R.string.info_img).isEmpty()) {//没有图片地址，从这里读取一次
            String imgUrl = result_list.get(0).attr("src");
            //  "https://www.biqugg.com"
            if (0 == uri.indexOf(IReader.资源笔趣阁2))
                imgUrl = IReader.资源笔趣阁2 + imgUrl;
            else if (0 == uri.indexOf(IReader.资源笔趣阁3))
                imgUrl = IReader.资源笔趣阁3 + imgUrl;
            else if (0 == uri.indexOf(IReader.资源笔趣阁4))
                imgUrl = IReader.资源笔趣阁4 + imgUrl;
            bookinfo.setFieldValue(R.string.info_img, imgUrl);
        }
        result_list = doc.select("div[id=intro]");
        if (null != result_list && result_list.size() > 0 && bookinfo.getFieldValue(R.string.info_introduction).isEmpty())
            bookinfo.setFieldValue(R.string.info_introduction, result_list.get(0).text());
        return true;
    }


    public abstract void runOnUiThread(int position);
}